<template>
  <el-dialog
    title="登录"
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :show-close="false"
  >
    <el-form :model="form">
      <el-form-item label="用户名称" :label-width="formLabelWidth">
        <el-input v-model="user.usrName"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" :label-width="formLabelWidth">
        <el-input type="password" v-model="user.usrPassword"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="login">登录</el-button>
    </div>
  </el-dialog>
</template>
  <script>
export default {
  data() {
    return {
      user: { usrName: "", usrPassword: "" },
      dialogFormVisible: true,
      form: {},
      formLabelWidth: "120px",
    };
  },
  methods: {
    login() {
      //可以在main.js中统一设置请求的地址axios.defaults.baseURL = 'http://localhost:8001'
      const url = "/login/dologin";
      this.$ajax
        .post(url, this.user)
        .then((res) => {
          console.log(res.data);
          if (res.data.errorCode == -1) {
            this.$message.error("用户名不存在");
          } else if (res.data.errorCode == -2) {
            this.$message.error("密码不正确");
          } else {
            console.log("登录正确");
            window.sessionStorage.setItem("usrId", res.data.data.usrId);
            window.sessionStorage.setItem("user", res.data.data.usrName);
            window.localStorage.setItem("token", res.data.token);
            this.$router.push("/welcome");
          }
          //this.dialogFormVisible = false;
        })
        .catch((error) => {
          this.$message.error("用户名不正确");
        });
    },
  },
};
</script>
  <style>
</style>